package elicecn.wohome.ftp;

public class Client {
	public static final String LOCATION_ID_HUBEI = "0142";
	public static final String FILE_TYPE = "CSV";
	public static final String COMPRESSION_FORMAT = "";

	public static final String ACTION_SAVE = "01";
	public static final String ACTION_DELETE = "03";

	// 解释返回值为Map<String, String>
	public static final String RETURN_RETURNCODE = "returncode";
	public static final String RETURN_ERRORMSG = "errormsg";
	public static final String RETURN_REQUEST_SN = "request_sn";
	public static final String RETURN_TASK_ID = "task_id";

	// 删除
	public String createRequestXmlOfDelete(String requestid, String request_sn,
			String import_file, String md5) {
		return createRequestXml(requestid, request_sn, ACTION_DELETE,
				import_file, FILE_TYPE, COMPRESSION_FORMAT, md5,
				LOCATION_ID_HUBEI);
	}

	// 保存
	public String createRequestXmlOfSave(String requestid, String request_sn,
			String import_file, String md5) {
		return createRequestXml(requestid, request_sn, ACTION_SAVE,
				import_file, FILE_TYPE, COMPRESSION_FORMAT, md5,
				LOCATION_ID_HUBEI);
	}

	/**
	 * 创建请求XML。默认值湖北、文件CSV、压缩类型空。
	 * 
	 * @param requestid
	 * @param request_sn
	 * @param action
	 * @param import_file
	 * @param md5
	 * @return
	 */
	public String createRequestXml(String requestid, String request_sn,
			String action, String import_file, String md5) {
		return createRequestXml(requestid, request_sn, action, import_file,
				FILE_TYPE, COMPRESSION_FORMAT, md5, LOCATION_ID_HUBEI);
	}

	/**
	 * 创建请求XML
	 * 
	 * @param requestid
	 *            访问者id 由导航平台提供， 必填;
	 * @param request_sn
	 *            请求序列号，由第三方平台提供，必填，最大128byte；
	 * @param action
	 *            处理动作：01保存，03删除；
	 * @param import_file
	 *            数据文件名称，最大128byte；
	 * @param file_type
	 *            文件类型:默认填CSV；
	 * @param compression_format
	 *            文件压缩格式:不填或ZIP；ZIP使用CRC-32算法;
	 * @param md5
	 *            文件内容的MD5摘要；
	 * @param location_id
	 *            本批数据所属省份地域编码;
	 * @return
	 */
	public String createRequestXml(String requestid, String request_sn,
			String action, String import_file, String file_type,
			String compression_format, String md5, String location_id) {
		StringBuffer buf = new StringBuffer();
		buf.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		buf.append("<request>");
		buf.append("<requestid>" + empty(requestid) + "<requestid>");
		buf.append("<request_sn>" + empty(request_sn) + "<request_sn>");
		buf.append("<retrieveargs>");
		buf.append("<action>" + empty(action) + "</action>");
		buf.append("<import_file>" + empty(import_file) + "</imort_file>");
		buf.append("<file_type>" + empty(file_type) + "</file_type>");
		buf.append("<compression_format>" + empty(compression_format)
				+ "</compression_format>");
		buf.append("<md5>" + empty(md5) + "</md5>");
		buf.append("<location_id>" + empty(location_id) + "</location_id>");
		buf.append("</retrieveargs>");
		buf.append("</request>");
		return buf.toString();
	}

	public static String empty(String str) {
		return str == null ? "" : str;
	}
}
